import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLDataException;
import java.sql.SQLException;

public class Demo1 {
    public static void main(String[] args) throws SQLException {
        //创建DataSouse
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/my_code?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //建立与服务器之间的连接

        Connection connection = dataSource.getConnection();

        //构造sql
        String sql1 = "insert into student values(1,'张三')";
        PreparedStatement statement = connection.prepareStatement(sql1);
        //把sql发送给服务器,返回值是一个整数，表示影响行数
        int n = statement.executeUpdate();
        System.out.println("n = " + n);
        //释放资源
        statement.close();
        connection.close();
    }
}
